Repository navigation
Offer codemode for high-effort chat research turns (staging only) - #1219
NielsRogge wants to merge 8 commits into
Conversation
Behind chat_codemode_enabled (off by default), sandbox-free MCP research turns get Pi 1.0's codemode tool. The 21 PwC catalog tools are registered but deactivated, so the model sees only their typed declarations inside the codemode description and calls them from QuickJS scripts, which receive structured results. Only a script's output reaches the model. Nested calls still pass the tool policy: each counts against the call and search budgets, while the script itself counts one round. The worker tags their tool_call_start events with parent_id, the backend validates and persists it, and the chat UI groups them under one codemode card that shows each lookup live and folds the script and its output away. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… tools Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Staging results (2026-11-27)Staging runs The same 8 reference research prompts were run twice each on Luna at medium effort (synthetic staging users, concurrency 2):
Codemode as wired works, but with the v1 prompt Luna doesn't use it well. 43 of 59 scripts made exactly one catalog call, and most printed whole results ( Capacity: 8 concurrent codemode turns all completed (median 20.4 s). Memory use of all Pi worker processes (production and staging) rose by about 220 MB at peak, available host memory dipped by about 600 MB to 4.6 GB, and the 1-minute load average peaked at 2.6 on 8 vCPUs. QuickJS is not a capacity concern at this scale. The binding limits stay the OpenAI and MCP circuits. Open before this could ship:
|
Staging A/B showed Luna wrapping single catalog calls in scripts, printing whole results and using Promise.all, which added model round trips. The guidance now names the result fields, requires allSettled, bounds fan-out to the call budget, and includes a worked task-to-papers script checked against the live catalog. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Prompt v2 results (2026-11-27)v2 (
Read: with v2, codemode is modestly better: about 11% faster at the median, about 17% fewer input tokens, about 18% cheaper, and it gathers more evidence per turn. 16 turns per arm is a small sample, so these deltas are within plausible noise. A larger run, or a few days of staging traffic, should confirm them before production. Staging (codemode on) is being updated to |
Larger A/B (2026-11-27): 24 prompts × 2 per armThe prompts cover leaderboards (with size and open-weight constraints), latest papers, method explanations, reading a specific paper, conferences, organizations and frameworks. Luna at medium effort, concurrency 3, prompt v2. The on-arm ran on staging
A rough quality check gave about the same answers: 1,281 vs 1,342 characters, 4.4 vs 4.0 distinct cited papers, 0.8 vs 0.75 benchmarks, 1.0 vs 1.25 links, and 3 vs 2 answers without citations. Conclusions
The earlier 8-prompt run's −11% median latency did not replicate. It was noise. Staging is being switched back to codemode on, on the active release. The PR stays unmerged. |
The 48-turn staging A/B showed scripts fanning out into the shared 12-call budget: 20 of 28 failures were budget refusals of the last paper lookups. Script calls now spend a separate per-turn budget of 24 catalog calls and 1 MiB of returned results, below the hosted MCP server's 60 requests per minute per chat session. Each codemode script still counts as one call and round, and searches keep their own ceiling. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # backend/chat_sandbox_worker/pi_worker/src/policy.ts
A spent script budget now refuses further script catalog calls and new codemode scripts with its own message, instead of closing tooling for the whole turn, so web search and diagrams keep their own budgets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # backend/chat_sandbox_worker/pi_worker/src/policy.ts
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 1f49f5c. Configure here.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # backend/chat_sandbox_worker/pi_worker/src/policy.ts
Script budget A/B (2026-11-27)
Same 24 prompts × 2, Luna at medium effort, concurrency 3, on staging
Against the previous on-arm, with the shared 12-call budget: failed tool calls fell 64% (−96% … −20%), and there were no script-budget refusals at all (20 before). Catalog calls changed by +5.6% (−6% … +19%). Ten turns used more than 12 catalog calls, peaking at 20. The remaining failures are real: catalog misses (for example "Google DeepMind" as an organization name), one MCP 502, three script errors, and two turn-limit refusals of a further codemode call. Read: the budget fixed the refusals, and codemode now fails less often than direct calls while staying about 19% cheaper. Its catalog traffic is about 47% higher, and latency is not better: the mean is up 15%, but the CI still includes no change, so this run cannot say whether codemode is slower. Before production, it would be worth running one more pair of arms to settle latency, or capping script fan-out lower if latency matters more than evidence gathered. Staging keeps codemode on ( |
Latency settled: second A/B pair (2026-11-27)I ran a second pair with the script budget on staging
The second pair alone gives +16.7% (+7.4% … +27.1%) for total time, consistent with the first (+14.8%). The two off-arms differ from each other by 13%, so run-to-run drift is real, and the alternating order is what makes the pooled result trustworthy. Where the time goes: of the 3.2 s added per turn, about 1.4 s is extra model time (writing the script), and about 1.8 s is spent outside the model running about 50% more catalog calls, often as sequential chains through the per-turn concurrency gate of 3. Conclusion: for this chat with Luna, codemode trades about 16% more latency, and a slower first tool call, for about 19% lower cost and fewer failed calls. It does not reduce model round trips, because Luna already makes parallel direct calls. I would not ship it as the default for latency-sensitive chat. Options if the cost saving matters:
Staging is being switched back to codemode on ( |
Two paired staging A/B runs (96 turns per arm at medium effort) showed codemode adds about 16% latency for about 19% lower cost. Keep it for the efforts where cost matters more than latency, configurable through chat_codemode_reasoning_efforts (default high and max). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
High-effort A/B (2026-11-27)Same 24 prompts at high effort, on staging Incomplete run: in the second off run, 31 of 48 turns were refused at admission by staging's own OpenAI rate-limit circuit (
Read: high effort behaves like medium effort. Codemode costs about 10–15% more latency, saves about 15–19%, and makes about 54% more catalog calls. Model round trips are flat again. High effort writes about 20% more output tokens with codemode (the scripts), so the relative cost saving is a little smaller than at medium. In absolute terms, codemode adds about 3–4 s to a 27 s turn at high effort, against about 3 s on a 21 s turn at medium. The effort gate therefore doesn't change the trade-off. It only limits it to turns where users already accept longer waits. Whether about 15% cheaper is worth about 12% slower on those turns is a product call. Neither effort level shows a latency win. Staging is back on codemode (verified). The PR stays unmerged. |
|
Staging keeps codemode on by the user's choice (2026-11-27): |

Summary
Adds Pi's codemode to chat research turns, behind
chat_codemode_enabled(default off). It applies only to the reasoning efforts inchat_codemode_reasoning_efforts(defaulthighandmax). Staging only: do not merge without explicit production authorization (chat change).When a turn qualifies, sandbox-free MCP research turns get Pi 1.0's
codemodetool. No Pi upgrade, contract change or Modal image change is needed:codemodeand their typed declarations, and scripts calltools.pwc_*()in QuickJS and get structured results. Only the script's output reaches the model.search_web, Excalidraw and file tools stay direct calls. Poster and paper authoring turns never get codemode.<codemode_usage>treats a script as a whole research step. It includes a worked task → benchmarks → leaderboards → papers example checked against the live catalog, requiresallSettled, and asks for only the fields the answer cites.tool_call_startevents carryparent_id, which the backend validates and persists. The chat UI groups each script's lookups under one live codemode card, with the script and its output folded away.codemode_enabled) only for qualifying turns. With the setting off, behaviour is unchanged.Results on staging (Luna, 24 research prompts)
Two A/B pairs at medium effort, run in the order off → on → on → off, 96 turns per arm, paired by prompt with 95% bootstrap intervals:
Luna already makes parallel direct calls, so scripts don't save model round trips. They trade latency for cost and more evidence per turn. Hence the effort gate: codemode is offered only where cost matters more than latency. The comments on this PR have the full history, including prompt v1 (no win), the shared-budget refusals, and the budget fix.
Validation
codemode.test.tsruns a real Pi session with a fake Responses provider and a model-written parallel script. Unit tests cover the run frame, prompt, script budget, retries and byte limits. The full suite passes, apart from rare timing flakes in unrelated transcript and MCP web-search tests.external_pi/turn_runneralso fail onmain.State
Staging runs the staging line plus this PR, with
PWC_CHAT_CODEMODE_ENABLED=true(backupsstaging.env.bak-20261127-codemode*). Production is untouched.🤖 Generated with Claude Code